#include "CGamePlayState.h"
#include "CMainMenuState.h"
#include "CGamePauseState.h"
#include "CGame.h"
#include "CUnit.h"
#include "CPlayer.h"
#include "CXInput.h"
#include "CAnimationManager.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_DirectSound.h"
#include "SGD Wrappers/CSGD_WaveManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "CObjectManager.h"
#include "Mail System.h"
#include "Deliverables.h"
#include "CLevel.h"
#include "CGameOverState.h"
#include "CWinState.h"
#include "CEventSystem.h"
#include "CObjGenerator.h"
#include "CParticleManager.h"
#include "SGD Wrappers/CSGD_FModManager.h"
#include "CStoryState.h"
#include "CEmitter.h"
#include <string>
using std::string;


CGamePlayState::CGamePlayState()
{
	Type(MAIN);
	m_pCurrLevel = NULL;
	m_pFire.nCurrAnimation = 0;
	m_pFire.nCurrFrame = 0;
	m_pFire.nTimer = 0;
	m_bSecondLevel = false;

	m_pEmitter1 = NULL;
	m_pPlayer = NULL;

	m_nBGMusic = CSGD_FModManager::GetInstance()->LoadSound("Resource/Sound/Batallia Downs.mp3");

	m_bfItem.Init("Ludicrum_Reclamation_Alt.fnt");
}

void CGamePlayState::DoStuff()
{
	m_nSpawnCount[4] = 0;

	for(int i = 0; i < 4; i++)
	{
		m_nSpawnCount[i] = rand() % ( 10 - 5 +1 ) + 5;
		m_nSpawnCount[4] += m_nSpawnCount[i];
	}
}
CGamePlayState::~CGamePlayState()
{
}
CGamePlayState* CGamePlayState::GetInstance()
{
	static CGamePlayState instance;
	return &instance;
}
void CGamePlayState::Enter()
{
	CSGD_Direct3D::GetInstance()->Clear(0,0,0);
	CSGD_Direct3D::GetInstance()->DeviceBegin();
	CSGD_Direct3D::GetInstance()->SpriteBegin();

	m_bfItem.Init("Ludicrum_Reclamation_Alt.fnt");
	m_bfItem.DrawBitmapText("Loading...", 200, 200, 2.0f);
	m_pFire.ChangeAnimation(FIRE_PIT);

	CSGD_Direct3D::GetInstance()->SpriteEnd();
	CSGD_Direct3D::GetInstance()->DeviceEnd();
	CSGD_Direct3D::GetInstance()->Present();

	if(m_bSecondLevel)
		CStoryState::GetInstance()->Load("story_2.png");
	else
		CStoryState::GetInstance()->Load("story_1.png");
	CGame::GetInstance()->PushState(CStoryState::GetInstance());

	ObjectGenerator<string, CBase>::GetInstance()->RegisterClassType<CBase>("CBase");
	ObjectGenerator<string, CBase>::GetInstance()->RegisterClassType<CUnit>("CUnit");


	m_pCurrLevel = new CLevel;
	m_pCurrLevel->Enter("Levels/map1.rmf");
	m_pCurrLevel->SetCurrArea(CITY_NE);


	MailSystem::GetInstance()->InitMessageSystem(CGame::GetInstance()->MessageProc);

	MailSystem::GetInstance()->ReadMail();
	m_fTimer = 420;
	DoStuff();
	

	if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBGMusic))
	{
		CSGD_FModManager::GetInstance()->PlaySound(m_nBGMusic);
		CSGD_FModManager::GetInstance()->SetVolume(m_nBGMusic, CGame::GetInstance()->MusicVol());
	}

	/////////////////////////////////////////////////////////////////
	// BUG FIX
	// Reference Bug # BB-011
	// Approved by:
	// BUG FIX START
	/////////////////////////////////////////////////////////////////
	CPlayer::GetInstance()->SetPosX(200);
	CPlayer::GetInstance()->SetPosY(200);
	///////////////////////////////////////////////////////////////////////////
	// BUG FIX END  Reference # BB-011
	//////////////////////////////////////////////////////////////////////////

}
bool CGamePlayState::Input()
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_ESCAPE) || CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_START)
	{
		CGame::GetInstance()->PushState(CGamePauseState::GetInstance());
	}
	
	return true;
}
void CGamePlayState::Exit()
{
	MailSystem::GetInstance()->ShutdownMailSystem();
	ObjectGenerator<string, CBase>::GetInstance()->UnregisterClassType("CBase");
	ObjectGenerator<string, CBase>::GetInstance()->UnregisterClassType("CUnit");
	ObjectGenerator<string, CBase>::GetInstance()->ShutdownObjectFactory();
	CParticleManager::GetInstance()->Shutdown();
	m_pCurrLevel->Exit();
	delete m_pCurrLevel;
	m_pCurrLevel = NULL;
	CObjectManager::GetInstance()->Clear();
	if(m_pPlayer != NULL)
		m_pPlayer = NULL;

	if(CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBGMusic))
		CSGD_FModManager::GetInstance()->StopSound(m_nBGMusic);
}
void CGamePlayState::Update(float fDelta)
{
	if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBGMusic))
		CSGD_FModManager::GetInstance()->PlaySound(m_nBGMusic);
	/////////////////////////////////////////////////////////////////
	// BUG FIX
	// Reference Bug # BB-008 && BB - 014
	// Approved by:
	// BUG FIX START
	/////////////////////////////////////////////////////////////////
	if(CGame::GetInstance()->IsPaused() == true)
		CGame::GetInstance()->PushState(CGamePauseState::GetInstance());
	///////////////////////////////////////////////////////////////////////////
	// BUG FIX END  Reference # BB-008 && BB - 014
	//////////////////////////////////////////////////////////////////////////


	if(m_pPlayer == NULL)
	{
		m_pPlayer = CPlayer::GetInstance();
		CObjectManager::GetInstance()->AddObject(m_pPlayer);
		CPlayer::GetInstance()->SetPosX((float)(CGame::GetInstance()->GetScreenWidth() - 100));
		CPlayer::GetInstance()->SetPosY((float)(CGame::GetInstance()->GetScreenHeight()/2) + 60);
		for(unsigned int i = 0; i < m_pCurrLevel->Spawnpoints().size(); ++i)
		{
			if(SecondLevel())
				MailSystem::GetInstance()->SendMail(new SpawnUnit(CHILD));
			else
				MailSystem::GetInstance()->SendMail(new SpawnUnit(RAT));
		}
		m_pCurrLevel->Enter("Levels/map1.rmf");
	}

	if(m_fTimer > 0)
	{
		m_fTimer -= fDelta;
		if(m_fTimer <= 1)
			CGame::GetInstance()->PushState(CGameOverState::GetInstance());
	}

	CParticleManager::GetInstance()->Update(fDelta);
	CAnimationManager::GetInstance()->Update(fDelta, &m_pFire);
	MailSystem::GetInstance()->ReadMail();
	CEventSystem::GetInstance()->ProcessEvents();
	CObjectManager::GetInstance()->Update(fDelta);
	CObjectManager::GetInstance()->CheckCollisons();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	if(m_pCurrLevel->GetCurrArea() == CITY_NE)
	{
		if(CPlayer::GetInstance()->GetPosX() <= 10)
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosX((float)(CGame::GetInstance()->GetScreenWidth() - 30));
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/map4.rmf");
			m_pCurrLevel->SetCurrArea(CITY_NW);

			for(int i = 0; i < m_nSpawnCount[0]; ++i)
			{
				if(SecondLevel())
					MailSystem::GetInstance()->SendMail(new SpawnUnit(CHILD));
				else
					MailSystem::GetInstance()->SendMail(new SpawnUnit(RAT));
			}
			
		}
		else if((CPlayer::GetInstance()->GetPosY() + CPlayer::GetInstance()->GetHeight())  >= CGame::GetInstance()->GetScreenHeight())
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosY(50);
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/map2.rmf");
			m_pCurrLevel->SetCurrArea(CITY_SE);
			for(int i = 0; i < m_nSpawnCount[0]; ++i)
			{
				if(SecondLevel())
					MailSystem::GetInstance()->SendMail(new SpawnUnit(CHILD));
				else
					MailSystem::GetInstance()->SendMail(new SpawnUnit(RAT));
			}
		}
		else if((CPlayer::GetInstance()->GetPosX() + CPlayer::GetInstance()->GetWidth()) >= CGame::GetInstance()->GetScreenWidth())
		{
			if(CPlayer::GetInstance()->GetPosY() >= 325 && (CPlayer::GetInstance()->GetPosY() + CPlayer::GetInstance()->GetHeight() <= 425))
			{
				if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
					CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
				if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
					CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
				if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
					CPlayer::GetInstance()->GetRed()->PlayEffect(false);
				if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
					CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
				m_pCurrLevel->Exit();
				CObjectManager::GetInstance()->CheckUnits();
				CObjectManager::GetInstance()->AddObject(m_pPlayer);
				CPlayer::GetInstance()->SetPosX(20);
				CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
				m_pCurrLevel->Enter("Levels/mapLakeArea.rmf");
				m_pCurrLevel->SetCurrArea(LAKE_AREA);
			}
		}
	}
	else if(m_pCurrLevel->GetCurrArea() == CITY_SE)
	{
		if((CPlayer::GetInstance()->GetPosY() - CPlayer::GetInstance()->GetHeight())  <= 10)
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosY((float)(CGame::GetInstance()->GetScreenHeight() - 30));
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/map1.rmf");
			m_pCurrLevel->SetCurrArea(CITY_NE);
			for(int i = 0; i < m_nSpawnCount[1]; ++i)
			{
				if(SecondLevel())
					MailSystem::GetInstance()->SendMail(new SpawnUnit(CHILD));
				else
					MailSystem::GetInstance()->SendMail(new SpawnUnit(RAT));
			}
		}
		else if((CPlayer::GetInstance()->GetPosX() - CPlayer::GetInstance()->GetWidth()) <= 10)
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosX((float)(CGame::GetInstance()->GetScreenWidth() - 20));
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/map3.rmf");
			m_pCurrLevel->SetCurrArea(CITY_SW);
			for(int i = 0; i < m_nSpawnCount[1]; ++i)
			{
				if(SecondLevel())
					MailSystem::GetInstance()->SendMail(new SpawnUnit(CHILD));
				else
					MailSystem::GetInstance()->SendMail(new SpawnUnit(RAT));
			}
		}
	}
	else if(m_pCurrLevel->GetCurrArea() == CITY_NW)
	{
		if((CPlayer::GetInstance()->GetPosX() + CPlayer::GetInstance()->GetWidth()) >= CGame::GetInstance()->GetScreenWidth())
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosX(20);
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/map1.rmf");
			m_pCurrLevel->SetCurrArea(CITY_NE);
			for(int i = 0; i < m_nSpawnCount[2]; ++i)
			{
				if(SecondLevel())
					MailSystem::GetInstance()->SendMail(new SpawnUnit(CHILD));
				else
					MailSystem::GetInstance()->SendMail(new SpawnUnit(RAT));
			}
		}
		else if((CPlayer::GetInstance()->GetPosY() + CPlayer::GetInstance()->GetHeight())  >= CGame::GetInstance()->GetScreenHeight())
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosY(50);
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/map3.rmf");
			m_pCurrLevel->SetCurrArea(CITY_SW);
			for(int i = 0; i < m_nSpawnCount[2]; ++i)
			{
				if(SecondLevel())
					MailSystem::GetInstance()->SendMail(new SpawnUnit(CHILD));
				else
					MailSystem::GetInstance()->SendMail(new SpawnUnit(RAT));
			}
		}
		else if(CPlayer::GetInstance()->GetPosY() <= 0 && CPlayer::GetInstance()->GetPosX() >= (CGame::GetInstance()->GetScreenWidth() - 100))
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosY((float)(CGame::GetInstance()->GetScreenHeight() - 30));
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/mapForest1.rmf");
			m_pCurrLevel->SetCurrArea(FOREST_ONE);
		}
	}
	else if(m_pCurrLevel->GetCurrArea() == CITY_SW)
	{
		if((CPlayer::GetInstance()->GetPosX() + CPlayer::GetInstance()->GetWidth()) >= CGame::GetInstance()->GetScreenWidth())
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosX(40);
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/map2.rmf");
			m_pCurrLevel->SetCurrArea(CITY_SE);
			for(int i = 0; i < m_nSpawnCount[3]; ++i)
			{
				if(SecondLevel())
					MailSystem::GetInstance()->SendMail(new SpawnUnit(CHILD));
				else
					MailSystem::GetInstance()->SendMail(new SpawnUnit(RAT));
			}
		}
		else if(CPlayer::GetInstance()->GetPosY() <= 0)
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosY((float)(CGame::GetInstance()->GetScreenHeight() - 30));
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/map4.rmf");
			m_pCurrLevel->SetCurrArea(CITY_NW);
			for(int i = 0; i < m_nSpawnCount[3]; ++i)
			{
				if(SecondLevel())
					MailSystem::GetInstance()->SendMail(new SpawnUnit(CHILD));
				else
					MailSystem::GetInstance()->SendMail(new SpawnUnit(RAT));
			}
		}
	}
	else if(m_pCurrLevel->GetCurrArea() == FOREST_ONE)
	{
		if((CPlayer::GetInstance()->GetPosY() + CPlayer::GetInstance()->GetHeight()) >= CGame::GetInstance()->GetScreenHeight() && ((CPlayer::GetInstance()->GetPosX() + CPlayer::GetInstance()->GetWidth()) >= (CGame::GetInstance()->GetScreenWidth() - 100)))
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosY((float)(30));
			m_pCurrLevel->Enter("Levels/map4.rmf");
			m_pCurrLevel->SetCurrArea(CITY_NW);
			for(int i = 0; i < m_nSpawnCount[3]; ++i)
			{
				if(SecondLevel())
					MailSystem::GetInstance()->SendMail(new SpawnUnit(CHILD));
				else
					MailSystem::GetInstance()->SendMail(new SpawnUnit(RAT));
			}
		}
		else if(CPlayer::GetInstance()->GetPosX() <= 10)
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosX((float)(CGame::GetInstance()->GetScreenWidth() - 30));
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/mapForest2.rmf");
			m_pCurrLevel->SetCurrArea(FOREST_TWO);

		}
	}
	else if(m_pCurrLevel->GetCurrArea() == FOREST_TWO)
	{
		if((CPlayer::GetInstance()->GetPosX() + CPlayer::GetInstance()->GetWidth()) >= CGame::GetInstance()->GetScreenWidth())
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosX(40);
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/mapForest1.rmf");
			m_pCurrLevel->SetCurrArea(FOREST_ONE);
		}
		else if(CPlayer::GetInstance()->GetPosX() <= 0)
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosX((float)(CGame::GetInstance()->GetScreenWidth() - 30));
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/mapCross.rmf");
			m_pCurrLevel->SetCurrArea(CROSS);
		}
	}
	else if(m_pCurrLevel->GetCurrArea() == CROSS)
	{
		if((CPlayer::GetInstance()->GetPosX() + CPlayer::GetInstance()->GetWidth()) >= CGame::GetInstance()->GetScreenWidth())
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosX(40);
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/mapForest2.rmf");
			m_pCurrLevel->SetCurrArea(FOREST_TWO);
		}
		else if(CPlayer::GetInstance()->GetPosY() <= 0 && m_bSecondLevel)
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosY((float)(CGame::GetInstance()->GetScreenHeight() - 30));
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/mapCaveEntrance.rmf");
			m_pCurrLevel->SetCurrArea(CAVE);
		}
		else if((CPlayer::GetInstance()->GetPosY() + CPlayer::GetInstance()->GetHeight()) >= CGame::GetInstance()->GetScreenHeight())
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosY(30);
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/mapCliff.rmf");
			m_pCurrLevel->SetCurrArea(CLIFF);
		}

	}
	else if(m_pCurrLevel->GetCurrArea() == LAKE_AREA)
	{
		if(CPlayer::GetInstance()->GetPosX()  <= 10)
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosX((float)(CGame::GetInstance()->GetScreenWidth() - 30));
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/map1.rmf");
			m_pCurrLevel->SetCurrArea(CITY_NE);
			for(unsigned int i = 0; i < m_pCurrLevel->Spawnpoints().size(); ++i)
			{
				if(SecondLevel())
					MailSystem::GetInstance()->SendMail(new SpawnUnit(CHILD));
				else
					MailSystem::GetInstance()->SendMail(new SpawnUnit(RAT));
			}
		}
	}
	else if(m_pCurrLevel->GetCurrArea() == CLIFF)
	{
		if(CPlayer::GetInstance()->GetPosY() <= 0)
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosY((float)(CGame::GetInstance()->GetScreenHeight() - 30));
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/mapCross.rmf");
			m_pCurrLevel->SetCurrArea(CROSS);
		}
	}
	else if(m_pCurrLevel->GetCurrArea() == CAVE)
	{
		if((CPlayer::GetInstance()->GetPosY() + CPlayer::GetInstance()->GetHeight()) >= CGame::GetInstance()->GetScreenHeight())
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			m_pCurrLevel->Exit();
			CObjectManager::GetInstance()->CheckUnits();
			CObjectManager::GetInstance()->AddObject(m_pPlayer);
			CPlayer::GetInstance()->SetPosY(30);
			CObjectManager::GetInstance()->ResetUnitPos((int)CPlayer::GetInstance()->GetPosX(), (int)CPlayer::GetInstance()->GetPosY());
			m_pCurrLevel->Enter("Levels/mapCross.rmf");
			m_pCurrLevel->SetCurrArea(CROSS);
		}
		if((CPlayer::GetInstance()->GetPosY() + CPlayer::GetInstance()->GetHeight()) <= (CGame::GetInstance()->GetScreenHeight()/2) && m_bSecondLevel)
		{
			if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
				CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
				CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
				CPlayer::GetInstance()->GetRed()->PlayEffect(false);
			if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
				CPlayer::GetInstance()->GetYellow()->PlayEffect(false);
			CGame::GetInstance()->PushState(CWinState::GetInstance());
		}

	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	if(m_nSpawnCount[4] <= 0)
	{
		CGamePlayState::GetInstance()->m_bSecondLevel = !CGamePlayState::GetInstance()->m_bSecondLevel;
		if(CGamePlayState::GetInstance()->m_bSecondLevel)
		{
			CGamePlayState::GetInstance()->Exit();
			CGamePlayState::GetInstance()->DoStuff();
			CGamePlayState::GetInstance()->Enter();
		}
		else
			CGame::GetInstance()->PushState(CWinState::GetInstance());
	}
}
void CGamePlayState::Render()
{
	if(m_pCurrLevel != NULL)
		m_pCurrLevel->Render();
	/////////////////////////////////////////////////////////
	//Fire Pit
	for(int j = -30; j < Death().bottom - Death().top - 40; j += 30)
	{
		for(int i = -8; i < Death().right - Death().left - 5; i = i + 30)
		{
			CAnimationManager::GetInstance()->Render(Death().left + i, Death().top + j, 1, -1, m_pFire, false);
		}
	}
	/////////////////////////////////////////////////////////////
	
	static int nHalfScreenWidth = ((unsigned int)(CGame::GetInstance()->GetScreenWidth())>>1)-100;
	static char buffer[128];
	sprintf_s(buffer,_countof(buffer),"TIME LEFT: %d", (int)m_fTimer);
	m_bfItem.DrawBitmapText(buffer, nHalfScreenWidth, 20, .75f);
	if(m_bSecondLevel)
		sprintf_s(buffer,_countof(buffer),"CHILDREN LEFT: %d", m_nSpawnCount[4]);
	else
		sprintf_s(buffer,_countof(buffer),"RATS LEFT: %d", m_nSpawnCount[4]);
	m_bfItem.DrawBitmapText(buffer, nHalfScreenWidth + 200, 20, .75f);
}